package com.salon.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salon.server.entity.Beautician;
import com.baomidou.mybatisplus.extension.service.IService;
import com.salon.server.entity.BeauticianProject;
import com.salon.server.entity.Project;
import com.salon.server.mapper.BeauticianMapper;
import com.salon.server.mapper.BeauticianProjectMapper;
import com.salon.server.mapper.ProjectMapper;
import com.salon.server.model.request.BeauticianProjectRequest;
import com.salon.server.model.request.BeauticianRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Admin
* @description 针对表【beautician(美容师基本信息表)】的数据库操作Service
* @createDate 2025-08-27 16:13:44
*/
@Service
public class BeauticianService extends ServiceImpl<BeauticianMapper, Beautician> {

    @Autowired
    private BeauticianProjectMapper beauticianProjectMapper;

    @Autowired
    private ProjectMapper projectMapper;

    public List<Project> project(Beautician beautician) {

        List<BeauticianProject> beauticianProjectList = beauticianProjectMapper.selectList(new LambdaQueryWrapper<BeauticianProject>()
                .eq(BeauticianProject::getBeauticianId, beautician.getId()));

        return projectMapper.selectList(new LambdaQueryWrapper<Project>()
                .in(Project::getId, beauticianProjectList.stream().map(BeauticianProject::getProjectId).collect(Collectors.toList())));
    }

    public void updateProject(BeauticianProjectRequest beauticianProjectRequest) {

        beauticianProjectMapper.delete(new LambdaQueryWrapper<BeauticianProject>()
                .eq(BeauticianProject::getBeauticianId, beauticianProjectRequest.getBeauticianId()));

        beauticianProjectRequest.getProjectIds().forEach(projectId -> {
            BeauticianProject beauticianProject = new BeauticianProject();
            beauticianProject.setBeauticianId(beauticianProjectRequest.getBeauticianId());
            beauticianProject.setProjectId(projectId);
            beauticianProjectMapper.insert(beauticianProject);
        });
    }
}
